<?php
/**
 * Created by : Sandi Rizky Kharisma (sandi.informatics08@gmail.com)
 * This Class Generated for CRUD purpose only
 */
class m_barang extends CI_Model
{

    function __construct()
    {
        parent::__construct();
    }

    /*public function get($id_toko = null, $where = null, $limit = null, $offset = null)
    {
        $name_table = ($id_toko == null) ? 'barang' : "barang_$id_toko";
        $name_table2 = ($id_toko == null) ? 'barang_toko' : "barang_toko_$id_toko";

        $this->db->select("$name_table.*");
        $this->db->select("$name_table2.*");
        $this->db->select('jenis_barang.NAMA_JENIS_BARANG');
        $this->db->join('jenis_barang', "$name_table.ID_JENIS_BARANG = jenis_barang.ID_JENIS_BARANG");
        $this->db->join($name_table2, "$name_table.KODE_BARANG = $name_table2.KODE_BARANG");

        if($where != null) $this->db->where($where);
        if($limit != null && $offset != null) $this->db->limit($limit, $offset);
        return $this->db->get($name_table);
    }*/

    public function get($where = null, $limit = null, $offset = null){
        if ($where!=null) $this->db->where($where);
        if($limit != null && $offset != null) $this->db->limit($limit, $offset);
        return $this->db->get('barang');

    }

    function get_all($limit, $uri)
    {
        $result = $this->db->get('barang', $limit, $uri);
        if ($result->num_rows() > 0)
        {
            return $result->result_array();
        } else
        {
            return array();
        }
    }

    function get_one($id_toko, $id)
    {
        $name_table = ($id_toko == null) ? 'barang' : "barang_$id_toko";
        $name_table2 = ($id_toko == null) ? 'barang_toko' : "barang_toko_$id_toko";
        
        $this->db->select("$name_table.*");
        $this->db->select("$name_table2.*");
        $this->db->select('jenis_barang.NAMA_JENIS_BARANG');
        $this->db->join('jenis_barang', "$name_table.ID_JENIS_BARANG = jenis_barang.ID_JENIS_BARANG");
        $this->db->join($name_table2, "$name_table.KODE_BARANG = $name_table2.KODE_BARANG");

        $this->db->where($name_table.'.KODE_BARANG', $id);
        $result = $this->db->get($name_table);
        if ($result->num_rows() == 1)
        {
            return $result;
        } else
        {
            return array();
        }
    }

    function insert($id_toko)
    {
        $name_table = ($id_toko == null) ? 'barang' : "barang_$id_toko";
        
        $data = array(

                'ID_JENIS_BARANG' => $this->input->post('ID_JENIS_BARANG', TRUE),

                'PEMILIK_BARANG' => $this->input->post('PEMILIK_BARANG', TRUE),

                'NAMA_BARANG' => $this->input->post('NAMA_BARANG', TRUE),

                'MERK' => $this->input->post('MERK', TRUE),

                'NAMA_SATUAN' => $this->input->post('NAMA_SATUAN', TRUE),

                'MODE_TOKO' => $this->input->post('MODE_TOKO', TRUE),

        );
        $this->db->insert($name_table, $data);
        return $this->db->insert_id();
    }

    function update($id_toko, $id)
    {
        $name_table = ($id_toko == null) ? 'barang' : "barang_$id_toko";

        $data = array(

                'ID_JENIS_BARANG' => $this->input->post('ID_JENIS_BARANG', TRUE),

                'PEMILIK_BARANG' => $this->input->post('PEMILIK_BARANG', TRUE),

                'NAMA_BARANG' => $this->input->post('NAMA_BARANG', TRUE),

                'MERK' => $this->input->post('MERK', TRUE),

                'NAMA_SATUAN' => $this->input->post('NAMA_SATUAN', TRUE),

                'MODE_TOKO' => $this->input->post('MODE_TOKO', TRUE),

        );
        $this->db->where('KODE_BARANG', $id);
        return $this->db->update($name_table, $data);
    }

    function addstok_barang($idbarang,$jumlahadd)
    {
        $query = "update barang set JUMLAH_AKHIR=JUMLAH_AKHIR+$jumlahadd where ID_BARANG=$idbarang";
        $result = $this->db->query($query);
        return $result;
    }

    function delete($id)
    {
        $this->db->where('KODE_BARANG', $id);
        $this->db->delete('barang');
    }

}
?>
